package com.bytz.modules.cms.domain.impl.order;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.domain.util.PriceComputerUtil;
import com.bytz.modules.cms.order.model.OrderItemModel;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.product.model.ProductModel;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 订单价格处理器 4
 *
 * @author shanxuezhong
 */
public class MallOrderPriceProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {


    public MallOrderPriceProcessor(int sequence) {
        super(sequence);
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        OrderModel order = context.getOrder();
        Map<String, ProductModel> productInDbMap = context.getProductModelMap();

        // 根据订单类型核对价格
        boolean isProjectOrder = BooleanUtil.isTrue(context.getProjectOrder());

        // 核对商品总价
        BigDecimal totalPrice = BigDecimal.ZERO;
        // 核对预付比例
        BigDecimal totalPrepay = BigDecimal.ZERO;

        for (OrderItemModel item : order.getOrderItemList()) {
            // db查出来的产品
            ProductModel productInDb = productInDbMap.get(item.getProductNumber());

            if (productInDb == null) {
                throw new BytzBootException("产品【" + item.getProductNumber() + "】无法购买");
            }

            if (item.getProductNum() <= 0) {
                throw new BytzBootException("产品【" + item.getProductNumber() + "】的数量不正确");
            }
            if (productInDb.getMaxOrderQuantity() > 0 && productInDb.getMaxOrderQuantity() < item.getProductNum()) {
                throw new BytzBootException("产品【" + item.getProductNumber() + "】的数量超过上限");
            }

            // 价格表是否相同
            if (ObjectUtil.notEqual(item.getNoTaxPrice(), productInDb.getCalculatePrice())) {
                throw new BytzBootException("产品【" + item.getProductNumber() + "】税前价格有误");
            }

            // 计算含税价
            if (productInDb.getCalculatePrice() != null) {
                BigDecimal taxPrice = item.getNoTaxPrice().multiply(order.getTaxRate().add(BigDecimal.ONE));

                item.setIncludeTaxPrice(PriceComputerUtil.saveScale(taxPrice));
            } else {
                if (!isProjectOrder) {
                    throw new BytzBootException("产品【" + item.getProductNumber() + "】无价格不能下标准订单");
                }

                item.setIncludeTaxPrice(null);
            }

            // 计算总价
            BigDecimal totalTaxPrice;
            if (isProjectOrder) {
                if (item.getNegotiatePrice() == null) {
                    throw new BytzBootException("产品【" + item.getProductNumber() + "】协议价不能为空");
                }

                // 协议价 * 数量
                totalTaxPrice = item.getNegotiatePrice().multiply(new BigDecimal(item.getProductNum()));
            } else {
                // 含税价 * 数量
                totalTaxPrice = item.getIncludeTaxPrice().multiply(new BigDecimal(item.getProductNum()));
            }

            totalPrice = totalPrice.add(totalTaxPrice);

            // 预付含税总价 * 预付比例
            BigDecimal prepay = item.getTotalTaxPrice().multiply(productInDb.getPrepayRatio());
            totalPrepay = totalPrepay.add(prepay);

            item.setTotalTaxPrice(totalTaxPrice);

            // 添加交期 商品描述 和 产品线Code
            item.setProductId(productInDb.getId());
            item.setDeliveryDate(productInDb.getDeliveryDate());
            item.setProductDesc(productInDb.getMaterialDesc());
            item.setProductLineCode(productInDb.getProductLineCode());
        }

        // 计算总价
        order.setTotalPrice(PriceComputerUtil.scale(totalPrice));
        order.setPrepayRatio(PriceComputerUtil.divide(totalPrepay, totalPrice));
    }

}